package fr.web325.servlet.client.service;

import fr.web325.bean.User;
import fr.web325.dao.ClientDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@WebServlet("/shop/client/mine")
public class MineServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取用户订单 从session中获取
        HttpSession session = req.getSession();
        if(session.getAttribute("user") == null){
            // 跳转登录
            resp.sendRedirect("login.jsp");
            return;
        }
        User user = (User)session.getAttribute("user");
        // 通过用户ID保存订单列表
        try {
            List<Map<String,Object>> orderList =  new ClientDao().getMineOrders(user.getId());
            req.setAttribute("orderList",orderList);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        req.getRequestDispatcher("/shop/client/mine.jsp").forward(req,resp);
    }
}
